package handler

import (
	"context"
	"gitee.com/gopher2011/go-zero-gva/internal/logic/user"
	"gitee.com/gopher2011/go-zero-gva/internal/svc"
	"gitee.com/gopher2011/go-zero-gva/internal/types"
	"gitee.com/gopher2011/go-zero-gva/utils"
	"github.com/gogf/gf/util/gvalid"
	"net/http"

	"github.com/tal-tech/go-zero/rest/httpx"
)

func ChangePassword(ctx *svc.ServiceContext) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		var req types.ChangePasswordReq
		if err := httpx.Parse(r, &req); err != nil {
			httpx.Error(w, err)
			return
		}
		if err := gvalid.CheckStruct(context.TODO(), &req, nil); err != nil {
			httpx.Error(w, err)
			return
		}
		l := user.NewUser(r.Context(), ctx)
		resp, err := l.ChangePassword(req)
		if err != nil {
			httpx.Error(w, err)
		} else {
			httpx.OkJson(w, resp)
		}
	}
}

func GetUserList(ctx *svc.ServiceContext) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		var req types.PageReq
		if err := httpx.Parse(r, &req); err != nil {
			httpx.Error(w, err)
			return
		}
		if err := gvalid.CheckStruct(context.TODO(), &req, nil); err != nil {
			httpx.Error(w, err)
			return
		}
		l := user.NewUser(r.Context(), ctx)
		resp, err := l.GetUserList(req)
		if err != nil {
			httpx.Error(w, err)
		} else {
			httpx.OkJson(w, resp)
		}
	}
}

func SetUserAuthority(ctx *svc.ServiceContext) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		var req types.SetUserAuthReq
		if err := httpx.Parse(r, &req); err != nil {
			httpx.Error(w, err)
			return
		}
		if err := gvalid.CheckStruct(context.TODO(), &req, nil); err != nil {
			httpx.Error(w, err)
			return
		}
		l := user.NewUser(r.Context(), ctx)
		userId := utils.GetUserID(r) // 请求体中的用户id
		uuid := utils.GetUserUuid(r) // 请求体中的用户uuid
		claims := utils.GetUserInfo(r)
		resp, err := l.SetUserAuthority(userId,uuid,req.AuthorityId,claims,r)
		if err != nil {
			httpx.Error(w, err)
		} else {
			httpx.OkJson(w, resp)
		}
	}
}

func DeleteUser(ctx *svc.ServiceContext) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		var req types.GetByIdReq
		if err := httpx.Parse(r, &req); err != nil {
			httpx.Error(w, err)
			return
		}
		if err := gvalid.CheckStruct(context.TODO(), &req, nil); err != nil {
			httpx.Error(w, err)
			return
		}
		l := user.NewUser(r.Context(), ctx)
		resp, err := l.DeleteUser(req)
		if err != nil {
			httpx.Error(w, err)
		} else {
			httpx.OkJson(w, resp)
		}
	}
}

func SetUserInfo(ctx *svc.ServiceContext) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		var req types.SysUserReq
		if err := httpx.Parse(r, &req); err != nil {
			httpx.Error(w, err)
			return
		}
		if err := gvalid.CheckStruct(context.TODO(), &req, nil); err != nil {
			httpx.Error(w, err)
			return
		}
		l := user.NewUser(r.Context(), ctx)
		resp, err := l.SetUserInfo(req)
		if err != nil {
			httpx.Error(w, err)
		} else {
			httpx.OkJson(w, resp)
		}
	}
}

func SetUserAuthorities(ctx *svc.ServiceContext) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		var req types.SetUserAuthoritiesReq
		if err := httpx.Parse(r, &req); err != nil {
			httpx.Error(w, err)
			return
		}
		if err := gvalid.CheckStruct(context.TODO(), &req, nil); err != nil {
			httpx.Error(w, err)
			return
		}
		l := user.NewUser(r.Context(), ctx)
		resp, err := l.SetUserAuthorities(req)
		if err != nil {
			httpx.Error(w, err)
		} else {
			httpx.OkJson(w, resp)
		}
	}
}

func GetUserInfo(ctx *svc.ServiceContext) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		var req types.Request
		if err := httpx.Parse(r, &req); err != nil {
			httpx.Error(w, err)
			return
		}
		if err := gvalid.CheckStruct(context.TODO(), &req, nil); err != nil {
			httpx.Error(w, err)
			return
		}
		l := user.NewUser(r.Context(), ctx)
		resp, err := l.GetUserInfo(req)
		if err != nil {
			httpx.Error(w, err)
		} else {
			httpx.OkJson(w, resp)
		}
	}
}


